import 'dart:convert';

import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/config.dart';
import 'package:yp/pages/trolley/go_collect/state.dart';
import 'package:yp/services/api.dart';

import '../../../helper.dart';
import '../trolley_logic.dart';

class GoCollectLogic extends GetxController {
  GoCollectState state = GoCollectState();
  var locationResult;
  RxInt cartItemsNum = Get.put(TrolleyLogic()).cartItemsNum;

  @override
  void onInit() {
    super.onInit();
    state.cornerDetail = Get.arguments['cornerDetail'];
    state.reservedNo = Get.arguments['reservedNo'];
    // locationResult = SpUtil.getObject(Config.USER_CURRENT_LOCATION);
    // print('--------------------------');
    // print(Get.arguments);
    // print(locationResult);
    // print(locationResult['AMapResult']);
    // surroundingSearch(null);
    getCollectList();
  }

  /// 获取换购列表
  void getCollectList() {
    // if (locationResult == null && locationResult['defaultAddress'] == null)
    //   return;
    // var defaultAddress = locationResult['defaultAddress'];
    var params = {
      "channel_num_id": 99,
      "use_platform": 2,
      "reserved_no": state.reservedNo
    };
    Api.goCollect(params).listen((event) {
      print('---------------------------------');
      print('event------$event');
      if (event != null && event['items'] != null) {
        state.exchangeList = event['items'];
      }
      update();
    });
  }

  /// 周边搜索
  // void surroundingSearch(String keywords) {
  //   if (locationResult == null) return;
  //   var aMapResult = locationResult['AMapResult'];
  //   Map<String, dynamic> params = {
  //     "use_platform": 2,
  //     "maplocation_x": aMapResult["longitude"],
  //     "maplocation_y": aMapResult["latitude"],
  //     "city": aMapResult["city"],
  //     "key_words": keywords
  //   };
  //   Api.surroundingSearch(params).listen((event) {
  //     print("周边搜索==========$event");
  //     if (event["code"] == 0 && event["address_list"] != null) {
  //       List addressList = event["address_list"];
  //       if (addressList.length > 0 && locationResult["userAddress"] == null) {
  //         locationResult["defaultAddress"] = addressList.first;
  //         getCollectList();
  //       }
  //     }
  //     update();
  //   });
  // }

  // 加入购物车
  void addShoppingCart({Map<String, dynamic> item}) {
    // if (locationResult == null && locationResult['defaultAddress'] == null)
    //   return;
    // var defaultAddress = locationResult['defaultAddress'];
    /// 小程序测试参数:
    // var params = {
    //   "data_sign": 0,
    //   "channel_num_id": 98,
    //   "use_platform": 3,
    //   "quantity": 1,
    //   "obj_type": "item",
    //   "mode": "cart",
    //   "prv_num_id": "440000",
    //   "city_num_id": "440100",
    //   "city_area_num_id": "440106",
    //   "access_token": "31ac66deeb81433991fe99f146d26313",
    //   "ec_shop_num_id": item['shop_id'],
    //   "item_num_id": item['item_num_id'],
    //   "ec_style_num_id": item['ec_style_num_id'],
    //   "style_num_id": item['style_num_id'],
    //   "usr_num_id": 1146,
    // };
    var params = {
      "use_platform": 2, //平台
      "usr_num_id": SpUtil.getInt('empeNumId'), //会员ID
      "emp_num_id": 0, //操作员ID,移动端无属性,默认传0
      "channel_num_id": 99, //订单渠道：小程序 98，App 99
      "quantity": 1.0, //数量
      "ec_shop_num_id": item['shop_id'], //店铺编码
      "item_num_id": item['item_num_id'], //商品编码
      "style_num_id": item['style_num_id'], //款式编码
      "ec_style_num_id": item['ec_style_num_id'], //款式唯一编码
      'access_token': SpUtil.getString('token'),
      // 'prv_num_id': defaultAddress['p_code'],
      // 'city_num_id': defaultAddress['city_code'],
      // 'city_area_num_id': defaultAddress['ad_code'],
    };
    Api.addShoppingCart(params).listen((event) {
      if (event['code'] == 0) {
        // MainAppLogics.getCartNum();
        getCartNum(item['shop_id']);
        toast('添加成功');
      } else {
        toast(event['message']);
      }
    });
  }

  getCartNum(shopId) {
    var params2 = {
      "usr_num_id": SpUtil.getInt('empeNumId'),
      "use_platform": 2,
      'access_token': SpUtil.getString('token'),
    };
    // var params2 = {
    //   "data_sign": 0,
    //   "channel_num_id": 98,
    //   "use_platform": 3,
    //   "access_token": "31ac66deeb81433991fe99f146d26313",
    //   "shop_id": 102002001
    // };
    // 购物车商品件数
    Api.cartItemNum(params2).listen((event) {
      // 购物车不同商品数量同步
      cartItemsNum.value = event["variety"];
      update();
    });
  }
}
